“The Instacart Online Grocery Shopping Dataset 2017”, Accessed from https://www.instacart.com/datasets/grocery-shopping-2017 on June 24, 2017.

“The Instacart Online Grocery Shopping Dataset 2017” is an anonymized dataset with over 3 million online grocery orders from more than 200,000 Instacart users. However the dataset does not represent a random sampling of products, users, or purchases. Therefore, while the data allow examination of trends in online grocery purchasing, the results may not be generalizable to Instacart users more broadly.


Chart A

Question. How different the sales are along each aisle? –> Answer.Based on the the plot, stuffs from the “chips pretzels’ aisle have been sold the most.”Ice cream toppings" corner was the least sold category/aisle.


Chart B

Question: Is the order number of product (the frequency purchase) associated with the order it added to cart? –> Answer: At first, I expected people add the stuffs that they’ve ordered many times before first. But, based on the graph, it doesn’t seem there is an noticeable association between the frequency of previous purchase and the order product added to cart.

Chart C

Question. Which snacks are purchased more than 500 times? and Is there any difference in purchase pattern? (e.g. the day of week the snack sold the most) –> Answer: Among the 13 snakcs purchased more than 500 times, Original Veggie Straws and Trail Mix show different pattern from the others. In general, snacks are purchased from Monday to Friday, with Wednesday as median. However, Original Veggie Straws were mainly purchased between Sunday to Thursday. Also, Trail Mix were mainly purchased between Monday to Thursday.

rmarkdown::render(“HW5_Q2_Dashboard.Rmd”, output_format = “flexdashboard::flex_dashboard”)

---
title: "HW5-Q2-Plotly"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: scroll
    source: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(labelled)
```

“The Instacart Online Grocery Shopping Dataset 2017”, Accessed from https://www.instacart.com/datasets/grocery-shopping-2017 on June 24, 2017.

“The Instacart Online Grocery Shopping Dataset 2017” is an anonymized dataset with over 3 million online grocery orders from more than 200,000 Instacart users. However the dataset does not represent a random sampling of products, users, or purchases. Therefore, while the data allow examination of trends in online grocery purchasing, the results may not be generalizable to Instacart users more broadly.

```{r}
data("instacart")
instacart = instacart %>%
  filter(department=="snacks") 

```

-----------------------------------------------------------------------

### Chart A

Question. How different the sales are along each aisle? 
--> Answer.Based on the the plot, stuffs from the "chips pretzels' aisle have been sold the most.  "Ice cream toppings" corner was the least sold category/aisle. 

```{r}
instacart %>% 
  count(aisle) %>% 
  mutate(aisle = fct_reorder(aisle, n)) %>% 
  plot_ly(x = ~aisle, y = ~n, color = ~aisle, type = "bar", colors = "viridis")
```


-----------------------------------------------------------------------

### Chart B

Question: Is the order number of product (the frequency purchase) associated with the order it added to cart? 
--> Answer: At first, I expected people add the stuffs that they've ordered many times before first. But, based on the graph, it doesn't seem there is an noticeable association between the frequency of previous purchase and the order product added to cart.

```{r}
instacart %>%
  plot_ly(
    x = ~order_number, y = ~add_to_cart_order, type= "scatter", color=~aisle)
```

### Chart C

Question. Which snacks are purchased more than 500 times? and Is there any difference in purchase pattern? (e.g. the day of week the snack sold the most) 
--> Answer: Among the 13 snakcs purchased more than 500 times, Original Veggie Straws and Trail Mix show different pattern from the others. In general, snacks are purchased from Monday to Friday, with Wednesday as median. However, Original Veggie Straws were mainly purchased between Sunday to Thursday. Also, Trail Mix were mainly purchased between Monday to Thursday. 

```{r}
instacart_snack = instacart %>% 
  count(product_name) %>% 
  mutate(product_name = fct_reorder(product_name,n)) 
instacart = 
  left_join(instacart, instacart_snack)

instacart %>%
  filter(n>500) %>%
  set_value_labels(order_dow =c("Sun"=0, "Mon"=1, "Tue"=2, "Wed"=3, "Thr"=4, "Fri"=5, "Sat"=6)) %>%
  mutate_if(is.labelled, to_factor) %>%
  plot_ly(y = ~order_dow, color = ~product_name, type = "box", text= ~n)

```


rmarkdown::render("HW5_Q2_Dashboard.Rmd", output_format = "flexdashboard::flex_dashboard")